package com.nhjf.dao.teachers;

import com.nhjf.dao.BaseDao;
import com.nhjf.model.teachers.Courses;
import com.nhjf.pojo.qo.teachers.CoursesQo;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

@Repository
public class CoursesDao extends BaseDao<Courses,CoursesQo> {
	/**
	 * 课程显示
	 */


	@Override
	protected Criteria buildCriteria(Criteria criteria, CoursesQo qo) {
		if(qo!=null){
			//            criteria.setFetchMode("resourceList", FetchMode.SELECT);
			criteria.setFetchMode("coursesSeries", qo.isSeachCoursesSeries()?FetchMode.JOIN:FetchMode.SELECT);	
			criteria.setFetchMode("uploader", qo.isSeachUploader()?FetchMode.JOIN:FetchMode.SELECT);	
			//id
			if(qo.getNotaAeries() !=null || qo.getIsReview()!=null || qo.getTypeOne()!=null || qo.getCourseSheries()!=null)  {
				criteria.createAlias("coursesSeries", "coursesSeries");
			}
			if(qo.getCoursesId()!=null){
				criteria.add(Restrictions.eq("id",qo.getCoursesId()));
			}
			//课程标题
			if(StringUtils.isNotBlank(qo.getTotalTitle())){
				criteria.add(Restrictions.like("coursetitle",qo.getTotalTitle(),MatchMode.ANYWHERE));
			}
			//发布时间
			if(qo.getReleasetime()!=null){
				criteria.add(Restrictions.eq("releasetime",qo.getReleasetime()));
			}

			//视频地址
			if(StringUtils.isNotBlank(qo.getVideo())){
				criteria.add(Restrictions.eq("video",qo.getVideo()));
			}

			if(qo.getNotaAeries()!=null) {
				criteria.add(Restrictions.eq("coursesSeries.isSeriesLesson",qo.getNotaAeries()));
			}
			if(qo.getTypeOne()!=null) {
				criteria.add(Restrictions.eq("coursesSeries.courseTypeOne",qo.getTypeOne().longValue()));
			}
			if(qo.getCourseSheries()!=null) {
				criteria.add(Restrictions.eq("coursesSeries.courseShare",qo.getCourseSheries()));
			}
			//是否审核
			if(qo.getIsReview()!=null) {
				criteria.add(Restrictions.eq("isReview",qo.getIsReview()));
			}
			if(qo.getSeriesId()!=null) {
				criteria.add(Restrictions.eq("coursesSeries.id",qo.getSeriesId()));
			}
			if(qo.isTimeSort()) {
				criteria.addOrder(Order.desc("releasetime"));
			}else {
				criteria.addOrder(Order.asc("coursetitle"));
			}
			if(qo.getUploaderId()!=null) {
				criteria.add(Restrictions.eq("uploader.id",qo.getUploaderId()));
			}
			if(qo.getIsPrice()!=null) {
				criteria.add(Restrictions.eq("isPay",qo.getIsPrice()));
			}
			if(StringUtils.isNotBlank(qo.getNickname())) {
				criteria.createAlias("uploader", "uploader");
				criteria.add(Restrictions.like("uploader.nickname",qo.getNickname(),MatchMode.ANYWHERE));
			}
			if(qo.getOrganizationId()!=null) {
				criteria.createAlias("uploader", "uploader");
				criteria.createAlias("uploader.traininginstitution", "uploader.traininginstitution");
				criteria.add(Restrictions.eq("uploader.traininginstitution.id",qo.getOrganizationId()));
			}

		}
		return criteria;
	}


	@Override
	protected Class<Courses> getEntityClass() {
		return Courses.class;
	}
}
